#-*- encoding: utf-8 -*-
from twisted.internet.protocol import Factory
from sys import stdout
import json
from twisted.protocols.basic import LineReceiver
from jxgame.globals.CmdGame import MDM_GR_LOGON, MDM_GR_USER, MDM_GR_MANAGER,\
    MDM_GR_SERVER_INFO, SUB_GR_LOGON_USERID
from jxgame.globals.GlobalFrame import MDM_GF_FRAME, MDM_GF_GAME
from jxgame.globals.CmdVideo import MDM_GF_VIDEO
from jxgame.game.land.Cmd import CMD_S_GameStart
from jxgame.server.gameservice.ServerUserManager import ServerUserManager
from jxgame.game.land.GameProtocol2 import GameProtocol2

class GameProtocol(LineReceiver):
    lastLine = ''
    serverUserManager = ServerUserManager()
    def connectionMade(self):
        self.transport.write(self.factory.quote + '\r\n')
#        self.transport.loseConnection()

    def lineReceived(self, line):
        if len(line) == 0:
            self.cmdReceived(self.lastLine)
            self.lastLine = line
        else:
            self.lastLine += line
        
    def cmdReceived(self,cmdLine):
        stdout.write("CMD_LINE:"  + cmdLine + "\r\n")
        if len(cmdLine) == 0:
            return
        cmd = json.loads(cmdLine)
        mainCmdID = cmd['MainCmdID']
        methods = {
           MDM_GR_LOGON: self.onMainLogin, 
           MDM_GR_USER: self.onMainUser,
           MDM_GR_MANAGER: self.onMainManager,
           MDM_GF_FRAME: self.onMainFrame,
           MDM_GF_GAME: self.onMainGame,
           MDM_GF_VIDEO: self.onMainVideo,
           MDM_GR_SERVER_INFO: self.onMainServer
        }
        method = methods[mainCmdID]
        method(cmd)
        
    #房间登陆
    def onMainLogin(self, cmd):
        subCmdID= cmd['SubCmdID']
        loginCmd = cmd['CMD']
        if subCmdID == SUB_GR_LOGON_USERID:
            t = self.serverUserManager.SearchUser(loginCmd['UserID'])
            if t[0] == None:
                UserID = loginCmd['UserID']
                Password = loginCmd['Password']
                resp = {}
                if UserID == 'test' and Password == '123':
                    resp['success'] = True
                    resp['server_info'] = {}                #配置信息
                    resp['table_info'] = {}                 #桌子信息                    
                else:
                    resp.success = False
                    resp.errorMsg = '用户名或密码错误'
                self.transport.write(json.dumps(resp))
            else:
                #在线处理
                if t[1] == 1:
                    pass
                #离线处理
                elif t[1] == 0:
                    pass
    
    def onMainUser(self, cmd):
        pass

    def onMainManager(self, cmd):
        pass
    
    def onMainFrame(self, cmd):
        pass
    
    def onMainGame(self, cmd):
        pass
    
    def onMainVideo(self, cmd):
        pass
    
    def onMainServer(self, cmd):
        pass

class GameProtocolFactory(Factory):
    protocol = GameProtocol2
    serverUserManager = ServerUserManager()

    def __init__(self, quote=None):
        self.quote = quote or 'An apple a day keeps the doctor away'        
        pass
    
    def onGameStart(self,cmdJson):
        cmd = CMD_S_GameStart()
        cmd.__dict__ = cmdJson
        
        
